package org.jivesoftware.smackx;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.DiscoverInfo.Identity;
import org.jivesoftware.smackx.packet.DiscoverItems;
import org.jivesoftware.smackx.packet.DiscoverItems.Item;

public class GatewayManager
{
  private static Map<Connection, GatewayManager> instances = new HashMap();
  private Connection connection;
  private Map<String, Gateway> gateways;
  private Map<String, Gateway> localGateways = new HashMap();
  private Map<String, Gateway> nonLocalGateways = new HashMap();
  private Roster roster;
  private ServiceDiscoveryManager sdManager;

  private GatewayManager()
  {
    this.gateways = new HashMap();
  }

  private GatewayManager(Connection paramConnection)
    throws XMPPException
  {
    this.gateways = new HashMap();
    this.connection = paramConnection;
    this.roster = paramConnection.getRoster();
    this.sdManager = ServiceDiscoveryManager.getInstanceFor(paramConnection);
  }

  private void discoverGateway(String paramString)
    throws XMPPException
  {
    DiscoverInfo localDiscoverInfo = this.sdManager.discoverInfo(paramString);
    Iterator localIterator = localDiscoverInfo.getIdentities();
    if (!(localIterator.hasNext()));
    while (true)
    {
      DiscoverInfo.Identity localIdentity;
      while (true)
      {
        do
        {
          return;
          localIdentity = (DiscoverInfo.Identity)localIterator.next();
        }
        while (!(localIdentity.getCategory().toLowerCase().equals("gateway")));
        this.gateways.put(paramString, new Gateway(this.connection, paramString));
        if (!(paramString.contains(this.connection.getHost())))
          break;
        this.localGateways.put(paramString, new Gateway(this.connection, paramString, localDiscoverInfo, localIdentity));
      }
      this.nonLocalGateways.put(paramString, new Gateway(this.connection, paramString, localDiscoverInfo, localIdentity));
    }
  }

  private void loadLocalGateways()
    throws XMPPException
  {
    Iterator localIterator = this.sdManager.discoverItems(this.connection.getHost()).getItems();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return;
      discoverGateway(((DiscoverItems.Item)localIterator.next()).getEntityID());
    }
  }

  private void loadNonLocalGateways()
    throws XMPPException
  {
    Iterator localIterator;
    if (this.roster != null)
      localIterator = this.roster.getEntries().iterator();
    while (true)
    {
      RosterEntry localRosterEntry;
      do
      {
        if (!(localIterator.hasNext()))
          return;
        localRosterEntry = (RosterEntry)localIterator.next();
      }
      while ((!(localRosterEntry.getUser().equalsIgnoreCase(StringUtils.parseServer(localRosterEntry.getUser())))) || (localRosterEntry.getUser().contains(this.connection.getHost())));
      discoverGateway(localRosterEntry.getUser());
    }
  }

  public Gateway getGateway(String paramString)
  {
    Object localObject;
    if (this.localGateways.containsKey(paramString))
      localObject = (Gateway)this.localGateways.get(paramString);
    while (true)
    {
      while (true)
      {
        return localObject;
        if (!(this.nonLocalGateways.containsKey(paramString)))
          break;
        localObject = (Gateway)this.nonLocalGateways.get(paramString);
      }
      if (!(this.gateways.containsKey(paramString)))
        break;
      localObject = (Gateway)this.gateways.get(paramString);
    }
    Gateway localGateway = new Gateway(this.connection, paramString);
    if (paramString.contains(this.connection.getHost()))
      this.localGateways.put(paramString, localGateway);
    while (true)
    {
      while (true)
      {
        this.gateways.put(paramString, localGateway);
        localObject = localGateway;
      }
      this.nonLocalGateways.put(paramString, localGateway);
    }
  }

  // ERROR //
  public GatewayManager getInstanceFor(Connection paramConnection)
    throws XMPPException
  {
    // Byte code:
    //   0: getstatic 26	org/jivesoftware/smackx/GatewayManager:instances	Ljava/util/Map;
    //   3: astore_2
    //   4: aload_2
    //   5: monitorenter
    //   6: getstatic 26	org/jivesoftware/smackx/GatewayManager:instances	Ljava/util/Map;
    //   9: aload_1
    //   10: invokeinterface 163 2 0
    //   15: ifeq +26 -> 41
    //   18: getstatic 26	org/jivesoftware/smackx/GatewayManager:instances	Ljava/util/Map;
    //   21: aload_1
    //   22: invokeinterface 167 2 0
    //   27: checkcast 2	org/jivesoftware/smackx/GatewayManager
    //   30: astore 7
    //   32: aload_2
    //   33: monitorexit
    //   34: aload 7
    //   36: astore 6
    //   38: goto +39 -> 77
    //   41: new 2	org/jivesoftware/smackx/GatewayManager
    //   44: dup
    //   45: aload_1
    //   46: invokespecial 170	org/jivesoftware/smackx/GatewayManager:<init>	(Lorg/jivesoftware/smack/Connection;)V
    //   49: astore 4
    //   51: getstatic 26	org/jivesoftware/smackx/GatewayManager:instances	Ljava/util/Map;
    //   54: aload_1
    //   55: aload 4
    //   57: invokeinterface 104 3 0
    //   62: pop
    //   63: aload_2
    //   64: monitorexit
    //   65: aload 4
    //   67: astore 6
    //   69: goto +8 -> 77
    //   72: astore_3
    //   73: aload_2
    //   74: monitorexit
    //   75: aload_3
    //   76: athrow
    //   77: aload 6
    //   79: areturn
    //
    // Exception table:
    //   from	to	target	type
    //   6	75	72	finally
  }

  public List<Gateway> getLocalGateways()
    throws XMPPException
  {
    if (this.localGateways.size() == 0)
      loadLocalGateways();
    return new ArrayList(this.localGateways.values());
  }

  public List<Gateway> getNonLocalGateways()
    throws XMPPException
  {
    if (this.nonLocalGateways.size() == 0)
      loadNonLocalGateways();
    return new ArrayList(this.nonLocalGateways.values());
  }

  public void refreshNonLocalGateways()
    throws XMPPException
  {
    loadNonLocalGateways();
  }
}